<?php

require_once("Model/LoginHandler.php");

class LoginView 
{
	//Privata strängar
	private $nameLocation = "userName"; //CoockiePlats
	private $passLocation = "password"; //CoockiePlats
	
	private $getUserName = "userName";
	private $getPassword = "password";
	private $getLoginKey = "login";
	private $getCheckbox = "checkbox";
	private $getLogoutKey = "logout";
	
	//Konstanter
	const LOGIN_CTRL_HEADER = 1;
	const LOGGED_IN_AS = 2;
	const LOGGED_OUT = 3;
	const INVALID_INFO = 4;

	public function RenderHeader($header)
	{
		switch($header)
		{
			case self::LOGIN_CTRL_HEADER;
				return "<h2>Login Controller</h2>";
				break;												
		}			
	}
	
	public function RenderMessage($message)
	{
		switch($message)
		{
			case self::LOGGED_IN_AS;
				return "</br>Inloggad som " . $this->GetUserName();
				break;

			case self::LOGGED_OUT;
				return "</br>Utloggad";
				break;

			case self::INVALID_INFO;
				return "</br>Fel: Felaktiga inloggningsuppgifter</br>";
				break;										
				
		}			
	}
 	
	//Sparar 2 coockies med anv. uppgifter
	public function UserDoRemember() 
	{	    		
		setcookie($this->nameLocation, $this->GetUserName(), time() + 60);
		setcookie($this->passLocation, $this->GetPassword(), time() + 60);
	}
	
	//Kollar om användar-coockies är satta
	public function DidUserRemember()
	{	
		if(isset($_COOKIE[$this->nameLocation]) == TRUE &&
			isset($_COOKIE[$this->passLocation]) == TRUE)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	//Rensar coockie-minne.	
	public function ClearMemory()
	{	
		setcookie($this->nameLocation, "", time() - 60);
		setcookie($this->passLocation, "", time() - 60);			
	}
		
	//Retunerar ett HTML formulär för inloggning
	public function DoLoginBox() 
	{
	    return $loginForm = 

	    "<form name='frmLoginUser' id='frmLoginUser' method='get'>	    		
			<p>Name:</p>			
			<input type='text' name='$this->getUserName' id='userName' size='50' /><br/>
			<p>Password:</p>							
			<input type='password' name='$this->getPassword' id='password' size='50' /><br/><br/>  			
			<input type='submit' value='Login' name='$this->getLoginKey' id='login' />	
			<input type='checkbox' value='check' name='$this->getCheckbox' id='check' /> Remember me			  			
		</form>";
	}
	
	//Retunerar ett HTML formulär för en utloggningsknapp		
	public function DoLogoutBox() 
	{
	    return $logoutButton =
	    
		"<form name='frmLoginUser' id='frmLoginUser' method='get'>
			<input type='submit' value='Logout' name='$this->getLogoutKey' id='logout' />	    	
		</form>";
				
	}
		
	//Retunerar användarnamnet om ett sådant finns, annars NULL  
	public function GetUserName() 
	{
		if(isset($_COOKIE[$this->nameLocation]) == TRUE)
		{
			return $_COOKIE[$this->nameLocation];
		}
	  	
		else if (isset($_GET[$this->getUserName]) == TRUE)
		{				 		
			return $_GET[$this->getUserName];
		}
		
		return NULL;		
	}
	
	//Retunerar lösenordet om ett sådant finns, annars null  
	public function GetPassword() 
	{
		if(isset($_COOKIE[$this->passLocation]) == TRUE)
		{
			return $_COOKIE[$this->passLocation];
		}
	    		
		if (isset($_GET[$this->getPassword]) == TRUE)
		{
			return $_GET[$this->getPassword];
		}
				
		return NULL;			
	}

	//Retunerar true om anv klickar i "Remember me"
	public function GetRememberBox() 
	{   			
		if (isset($_GET[$this->getCheckbox]))
		{   				
			return TRUE;
		}
		
		return FALSE; 		
	}	
	
	//Retunerar TRUE om anv klickar på loginknappen  
	public function TriedToLogin() 
	{	
		if (isset($_GET[$this->getLoginKey]) || $this->DidUserRemember() == TRUE)
		{   			
			return TRUE;  	
		}
				
		return FALSE;
	   		
	}
	
	//Retunerar TRUE om anv klickar på logoutknappen  
	public function TriedToLogOut() 
	{   			
		if (isset($_GET[$this->getLogoutKey]))
		{			
			return TRUE;
		}
		
		return FALSE; 		
	}
	
	//Fråga om alternativ lösning...
	/*public function DidUserRemember() 
	{	    		
		if (isset($_GET[$this->getCheckbox]) == TRUE)
		{
			setcookie($this->nameLocation, $this->GetUserName(), time() + 60);
			setcookie($this->passLocation, $this->GetPassword(), time() + 60);
		}	
		else if($this->TriedToLogOut())
		{
			setcookie($this->nameLocation, "", time() - 60);
			setcookie($this->passLocation, "", time() - 60);	
		}	
		else if(isset($_COOKIE[$this->nameLocation]) == TRUE &&
					isset($_COOKIE[$this->passLocation]) == TRUE)
		{
				return TRUE;
		}
		
		return FALSE;
	}*/
	   					       	
} 
  

?>
  